package org.tomcat.unit4;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Stack;

public class HttpConnector implements Runnable {

    private boolean stopped;

    private boolean started;

    private boolean initialized;

    private Thread thread;

    private ServerSocket serverSocket;

    private String threadName;

    private int port = 8080;

    private String scheme = "http";

    private Stack processors = new Stack();

    protected int minProcessors = 5;

    private int maxProcessors = 20;

    private int curProcessors = 0;

    public String getScheme() {
        return scheme;
    }

    @Override
    public void run() {
        while (!stopped) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();
                socket.setSoTimeout(120000);
                socket.setTcpNoDelay(true);
            } catch (IOException e) {
                e.printStackTrace();
            }

            HttpProcessor processor = createProcessor();
            if (processor == null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            processor.assign(socket);
        }
    }

    private HttpProcessor createProcessor() {
        synchronized (processors) {
            if (processors.size() > 0) {
                return ((HttpProcessor) processors.pop());
            }
            if ((maxProcessors > 0) && (curProcessors < maxProcessors)) {
                return (newProcessor());
            } else {
                if (maxProcessors < 0) {
                    return (newProcessor());
                } else {
                    return (null);
                }
            }
        }
    }

    public void start() {

        if (started) {
            throw new RuntimeException("tomcat started.");
        }
        threadName = "HttpConnector[" + port + "]";
        started = true;

        threadStart();

        while (curProcessors < minProcessors) {
            if ((maxProcessors > 0) && (curProcessors >= maxProcessors)) {
                break;
            }
            HttpProcessor processor = newProcessor();
            recycle(processor);
        }
    }

    public void initialize() throws Exception {
        if (initialized) {
            throw new Exception("httpConnector.alreadyInitialized");
        }
        this.initialized = true;
        try {
            serverSocket = open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ServerSocket open () throws Exception {
        serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
        return serverSocket;
    }

    private void threadStart() {
        thread = new Thread(this, threadName);
        thread.setDaemon(true);
        thread.start();
    }

    private HttpProcessor newProcessor() {
        HttpProcessor processor = new HttpProcessor(this, curProcessors++);
        return processor;
    }

    public void recycle(HttpProcessor processor) {
        processors.push(processor);
    }

    public void setMinProcessors(int minProcessors) {
        this.minProcessors = minProcessors;
    }

    public void setMaxProcessors(int maxProcessors) {
        this.maxProcessors = maxProcessors;
    }
}
